package com.air.service.impl;

import com.air.dao.CardMapper;
import com.air.dao.UserMapper;
import com.air.entity.Card;
import com.air.entity.User;
import com.air.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *  用户服务
 * @author 彭程
 */
@Transactional
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    CardMapper cardMapper;

    @Override
    public User selectUserById(long id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public User selectUserByName(String name) {
        return userMapper.selectUserByName(name);
    }

    @Override
    public User selectUserByCardId(String cardId) {
        return userMapper.selectUserByCardId(cardId);
    }

    @Override
    public User selectUserByEmail(String email) {
        return userMapper.selectUserByEmail(email);
    }

    @Override
    public User selectUserByPhone(String phone) {
        User user = userMapper.selectUserByPhone(phone);
        System.out.println("服务层"+user);
        return user;
    }

    @Override
    public boolean loginByEmail(String openId, String password) {
        User user = userMapper.selectUserByEmail(openId);
        if (user == null){
            return false;
        }
        return  user.getPassword().equals(password);
    }

    @Override
    public boolean loginByIdNumber(String openId, String password) {
        User user = userMapper.selectUserByCardId(openId);
        if (user == null){
            return false;
        }
        return  user.getPassword().equals(password);
    }

    @Override
    public boolean loginByPhone(String openId, String password) {
        User user = userMapper.selectUserByPhone(openId);
        if (user == null){
            return false;
        }
        return  user.getPassword().equals(password);
    }

    @Override
    public boolean loginService(String username, String password) {
        User user = userMapper.selectUserByName(username);
        if (user == null){
            return false;
        }
        return  user.getPassword().equals(password);
    }

    @Override
    public boolean insertUser(User user, Card card) {
        if (userMapper.insertUser(user)>0){
            int id = userMapper.selectIdByPhone(user.getPhone());
            System.out.println(id);
            card.setUserId(id);
            return cardMapper.insertCard(card)>0;
        }
        return false;
    }
}
